﻿﻿using System;
using System.Collections;
using System.Collections.Generic;

using UnityEngine;

namespace ALPackage
{
    /*********************
     * UI系统的层级管理根对象
     **/
    public class ALUILayerMgr
    {
        private static ALUILayerMgr _g_instance = new ALUILayerMgr();
        public static ALUILayerMgr instance
        {
            get
            {
                if (null == _g_instance)
                    _g_instance = new ALUILayerMgr();

                return _g_instance;
            }
        }

        /// <summary>
        /// 存储所有节点对应对象的映射关系表
        /// </summary>
        private Dictionary<int, _AALUILayerBasicNode> _m_dicAllNodeDic;


        protected ALUILayerMgr()
        {
            _m_dicAllNodeDic = new Dictionary<int, _AALUILayerBasicNode>();
        }

        /// <summary>
        /// 初始化所有UI根节点的节点对象
        /// </summary>
        public void initUIRootNodes(List<ALUILayerInfo> _uiLayerInfoList)
        {
            if(null == _uiLayerInfoList)
                return;

            //逐个注册
            ALUILayerInfo tmpObj = null;
            for(int i = 0; i < _uiLayerInfoList.Count; i++)
            {
                tmpObj = _uiLayerInfoList[i];
                if(null == tmpObj)
                    continue;

                if(null == tmpObj.layerRootGo || null == tmpObj.cavasObj)
                    continue;

                //判断是否有重复节点
                if(_m_dicAllNodeDic.ContainsKey(tmpObj.cavasObj.transform.GetInstanceID()))
                {
                    //有可能多个UI节点使用不同的node，所以这里跳过注册
                    continue;
                }

                //创建一个根节点对象
                ALUILayerRootNode rootNode = new ALUILayerRootNode(tmpObj);
                //添加一个节点对象
                _regUILayerNode(rootNode);
            }
        }

        /// <summary>
        /// 添加一个UI层级节点
        /// </summary>
        /// <param name="_node"></param>
        public void addUILayerNode(_AALUILayerBasicNode _node)
        {
            if(null == _node || null == _node.transF)
                return;

            //遍历查询是否有已经注册的父节点，如无则报错
            _AALUILayerBasicNode parentNode = null;
            Transform tmpParent = _node.transF.parent;
            while(true)
            {
                if(null == tmpParent)
                    break;

                //获取数据
                int parentInstanceId = tmpParent.GetInstanceID();
                if(_m_dicAllNodeDic.TryGetValue(parentInstanceId, out parentNode))
                {
                    if (!(parentNode is _IALUILayerParentNode))
                    {
                        //获取父节点
                        tmpParent = tmpParent.parent;
                        continue;
                    }

                    //进行后续处理
                    parentNode.addChild(_node);

                    //加入数据集
                    if(!_m_dicAllNodeDic.ContainsKey(_node.goInstanceID))
                    {
                        _m_dicAllNodeDic.Add(_node.goInstanceID, _node);
                    }

                    break;
                }

                //获取父节点
                tmpParent = tmpParent.parent;
            }

            //如果没数据则报错
            if(null == parentNode)
            {
                ALLog.Error("Can not find parent Node for: " + _node.transF.name);
            }
        }

        /// <summary>
        /// 添加一个动态管理层级的UI层级节点
        /// </summary>
        /// <param name="_node"></param>
        public void addUILayerDynamicNode(_AALUILayerBasicNode _node, int _additionLayer)
        {
            if(null == _node || null == _node.transF)
                return;

            //遍历查询是否有已经注册的父节点，如无则报错
            _AALUILayerBasicNode parentNode = null;
            Transform tmpParent = _node.transF.parent;
            while(true)
            {
                if(null == tmpParent)
                    break;

                //获取数据
                int parentInstanceId = tmpParent.GetInstanceID();
                if(_m_dicAllNodeDic.TryGetValue(parentInstanceId, out parentNode))
                {
                    if(!(parentNode is _IALUILayerDynamicParentNode))
                    {
                        //获取父节点
                        tmpParent = tmpParent.parent;
                        continue;
                    }

                    _IALUILayerDynamicParentNode dynamicNode = (_IALUILayerDynamicParentNode)parentNode;
                    //进行后续处理
                    dynamicNode.addDynamicChild(_node, _additionLayer);

                    //加入数据集
                    if(!_m_dicAllNodeDic.ContainsKey(_node.goInstanceID))
                    {
                        _m_dicAllNodeDic.Add(_node.goInstanceID, _node);
                    }

                    break;
                }

                //获取父节点
                tmpParent = tmpParent.parent;
            }

            //如果没数据则报错
            if(null == parentNode)
            {
                ALLog.Error("Can not find parent Node for: " + _node.transF.name);
            }
        }

        /// <summary>
        /// 从管理器中移除某个节点
        /// </summary>
        /// <param name="_node"></param>
        public void rmvLayerNode(int _instanceId)
        {
            _AALUILayerBasicNode node = null;
            if(!_m_dicAllNodeDic.TryGetValue(_instanceId, out node))
                return;

            //移除本节点
            _m_dicAllNodeDic.Remove(_instanceId);
            //移除子节点
            node.clearAllChild();

            //清理对应节点的数据
            if(null != node.parentNode)
                node.parentNode.rmvChild(node);
        }
        public void rmvLayerNode(_AALUILayerBasicNode _node)
        {
            if(null == _node || null == _node.transF)
                return;

            //移除本节点
            int instanceId = _node.goInstanceID;
            //移除本对象
            if(_m_dicAllNodeDic.Remove(instanceId))
            {
                //成功则移除所有子节点对象
                _node.clearAllChild();

                //清理对应节点的数据
                if(null != _node.parentNode)
                    _node.parentNode.rmvChild(_node);
            }
        }

        /// <summary>
        /// 获取对应Go的节点所对应的层级信息
        /// </summary>
        /// <param name="_instanceId"></param>
        /// <returns></returns>
        public _AALUILayerBasicNode getUILayerNode(int _instanceId)
        {
            _AALUILayerBasicNode resObj = null;
            if(_m_dicAllNodeDic.TryGetValue(_instanceId, out resObj))
                return resObj;

            return null;
        }

        /// <summary>
        /// 注册节点
        /// </summary>
        /// <param name="_node"></param>
        protected void _regUILayerNode(_AALUILayerBasicNode _node)
        {
            if(null == _node)
                return;

#if UNITY_EDITOR
            //判断是否有重复值
            if(_m_dicAllNodeDic.ContainsKey(_node.goInstanceID))
            {
                ALLog.Error("Node Has Been in the manager!");
                return;
            }
#endif

            _m_dicAllNodeDic.Add(_node.goInstanceID, _node);
        }
    }
}
